package api;

import com.alibaba.fastjson.JSON;
import service.ProductService;
import utils.Response;
import utils.ResponseCode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class HotWeekServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //分层：api：接受请求，提取请求参数，返回消息。service
        ProductService productService=new ProductService();

        Response build=null;
        List<Object[]> list=null;
        try {
            list=productService.getHotWeekProducts();//登陆成功，返回数据
            build=Response.build(ResponseCode.SUCCESS,list) ;
        }catch (SQLException e){
            e.printStackTrace();//登录异常，返回成功消息
            build= Response.build(ResponseCode.ERROR_DB);
        }
        //第四部：判断为空，生成登录失败的消息对象
        //第五步：把消息对象转换层字符串，返回浏览器
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSON(build).toString());
    }
}
